package com.gitee.wsl.struct.kdtree

import com.gitee.wsl.mathematics.vector.vec2.Vec2f
import com.gitee.wsl.mathematics.vector.vec3.Vec3
import com.gitee.wsl.mathematics.vector.vec4.Vec4
import kotlin.jvm.JvmName

/** built-in mapper for [Vec2f] */
fun vector2Mapper(v: Vec2f, dimension: Int): Double {
    return when (dimension) {
        0 -> v.x.toDouble()
        else -> v.y.toDouble()
    }
}

/*fun intVector2Mapper(v: IntVector2, dimension: Int): Double {
    return when (dimension) {
        0 -> v.x.toDouble()
        else -> v.y.toDouble()
    }
}*/


/** built-in mapper for [Vector3] */
fun vector3Mapper(v: Vec3, dimension: Int): Double {
    return when (dimension) {
        0 -> v.x
        1 -> v.y
        else -> v.z
    }
}

/** built-in mapper for [Vector4] */
fun vector4Mapper(v: Vec4, dimension: Int): Double {
    return when (dimension) {
        0 -> v.x
        1 -> v.y
        2 -> v.z
        else -> v.w
    }
}



@JvmName("kdTreeVector2")
fun Iterable<Vec2f>.kdTree(): KDTreeNode<Vec2f> {
    val items = this.toMutableList()
    return buildKDTree(items, 2, ::vector2Mapper)
}

@JvmName("kdTreeVector3")
fun Iterable<Vec3>.kdTree(): KDTreeNode<Vec3> {
    val items = this.toMutableList()
    return buildKDTree(items, 3, ::vector3Mapper)
}

@JvmName("kdTreeVector4")
fun Iterable<Vec4>.kdTree(): KDTreeNode<Vec4> {
    val items = this.toMutableList()
    return buildKDTree(items, 4, ::vector4Mapper)
}